

/**
 * popup提示框
 */

import '../cssStyle/Popup.css'

export default class Popup {

  constructor(viewer) {
    this._viewer = viewer
    this._wrapper = undefined
    this._enable = undefined
    this._template = undefined
    this._positions = undefined
    this.mounted()
  }

  /**
   *  初始化 
   */

  mounted() {
    this._wrapper = this.createDom()
  }

  /**
   * 创建dom元素
   * @returns dom元素
   */
  createDom() {
    let domElement = document.createElement('div')
    domElement.className = 'dk-popup'
    this._viewer.cesiumWidget.container.appendChild(domElement)
    return domElement
  }

  /** 
   * 
  */
  bindEvent(){
    this._viewer.scene.postRender.addEventListener(this.updateWindowPosition , this)
  }

  unbindEvent(){
    this._viewer.scene.postRender.removeEventListener(this.updateWindowPosition , this)
  }

  /**
   * 更新屏幕位置
   */
  updateWindowPosition() {
    var windowPosition = new Cesium.Cartesian2()
    windowPosition = Cesium.SceneTransforms.wgs84ToWindowCoordinates(this._viewer.scene , this._positions , new Cesium.Cartesian2())

    let x = windowPosition.x - this._wrapper.clientWidth / 2
    let y = windowPosition.y - this._wrapper.clientHeight - 10
    this._wrapper.style.transform = 'translate('+ x +'px , '+ y +'px)'
  }

  /**
   * 展示信息
   * @param {*} position 
   * @param {*} message 
   */
  showAt(position, message) {
    position && this.bindEvent()
    this._positions = position
    this._template = message
    this._wrapper.innerHTML = this._template
  }

  /**
   * 设置css样式
   * @param {*} style 
   */

  setCssStyle(style) {

  }

  /**
   * 设置html模板
   * @param {html} template 
   */
  setTemplate(template) {
    if (!template) return
    this._template = template
    this._wrapper.innerHTML = this._template
  }

  /**
   * 销毁对象
   */
  destroy() {
    this._wrapper.remove()
    this.unbindEvent()
  }

  get enable() {
    return this._enable
  }

  set enable(val) {
    this._enable = val
    let isShow = this._enable ? 'visible' : 'hidden'
    this._wrapper.style.visibility = isShow
    if(!isShow){
      this.unbindEvent()
    }
    isShow = undefined
  }
}

